package smartcampus.scjava.controller.forth;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import smartcampus.scjava.POJO.DO.Laboratory;
import smartcampus.scjava.POJO.DO.LaboratoryReserve;
import smartcampus.scjava.POJO.DO.LibrarySeat;
import smartcampus.scjava.POJO.DO.LibrarySeatReserve;
import smartcampus.scjava.POJO.DTO.LaboratoryReserveDTO;
import smartcampus.scjava.POJO.DTO.SeatReserveDTO;
import smartcampus.scjava.custom.ResultVO;
import smartcampus.scjava.service.ILaboratoryReserveService;
import smartcampus.scjava.service.ILaboratoryService;
import smartcampus.scjava.service.ILibrarySeatReserveService;
import smartcampus.scjava.service.ILibrarySeatService;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 实验室预定表 前端控制器
 * </p>
 *
 * @author xian
 * @since 2023-12-05
 */
@RestController
@RequestMapping("/laboratory-reserve")
public class LaboratoryReserveController {
    @Resource
    private ILaboratoryReserveService reserveService;

    @Resource
    private ILaboratoryService laboratoryService;

    @RequestMapping("/listPage")
    public ResultVO<?> listPage(Integer  pageNum, Integer pageSize, String word, String userId) {
        if (pageNum == null || pageSize == null) {
            return ResultVO.error("分页参数错误");
        }
        if (userId == null || userId.isEmpty()) {
            return ResultVO.error("userId不能为空");
        }
        Page<LaboratoryReserve> page = new Page<>(pageNum,pageSize);
        QueryWrapper<LaboratoryReserve> query = new QueryWrapper<>();
        query.lambda().eq(LaboratoryReserve::getUserId,userId);
        if (StringUtils.hasText(word)) {
            query.lambda().or().like(LaboratoryReserve::getLaboratoryNo, word)
                    .or().like(LaboratoryReserve::getLaboratoryName, word);
        }
        reserveService.page(page,query);

        return ResultVO.success(page);
    }

    @RequestMapping("/listReserveByLab")
    public ResultVO<?> listReserveByLab(Integer laboratoryId) {


        List<Map<String, Object>> maps = reserveService.listReserveByLab(laboratoryId);

        return ResultVO.success("成功",maps);
    }





    @RequestMapping("/add")
    public ResultVO<?> add(@RequestBody LaboratoryReserveDTO dto) {

        String timeString = dto.getTimeString();
        if (!StringUtils.hasText(timeString)) {
            return ResultVO.error("时间不能为空");
        }

        String[] times = timeString.split(",");
        LaboratoryReserve reserve = new LaboratoryReserve();
        if (times.length >= 1) {
            boolean check = reserveService.checkRepeatLabReserve(dto.getLaboratoryId(), times);
            if (!check) {
                return ResultVO.error("时间已被占用");
            }
            // times长度大于1时需要判断是否连续
            if (times.length > 1) {
                for (int i = 0; i < times.length - 1; i++) {
                    if (Integer.valueOf(times[i]) + 1 != Integer.valueOf(times[i + 1])) {
                        return ResultVO.error("时间不连续");
                    }
                }
            }

            Laboratory byId = laboratoryService.getById(dto.getLaboratoryId());
            if (byId == null) {
                return ResultVO.error("实验室不存在！");
            }
            reserve.setLaboratoryId(dto.getLaboratoryId());
            reserve.setLaboratoryNo(byId.getLaboratoryNo());
            reserve.setLaboratoryName(byId.getLaboratoryName());
            reserve.setUserId(dto.getUserId());
            reserve.setBeginTime(LocalDateTime.of(LocalDate.now(), LocalTime.of(Integer.parseInt(times[0]),0,0,0)));
            reserve.setEndTime(reserve.getBeginTime().plusHours(times.length));
            reserve.setReserveMinutes(times.length*60);
            reserve.setIsDelete(0);
            reserve.setCreateTime(LocalDateTime.now());
            reserve.setUpdateTime(LocalDateTime.now());
            reserveService.save(reserve);
        }

        return ResultVO.success("预约成功");
    }

    @RequestMapping("/delete")
    public ResultVO<?> delete(Integer reserveId) {
        reserveService.removeById(reserveId);
        return ResultVO.success("删除成功");
    }
}
